/*
state  存放数据
mutations 同步修改数据
actions  异步修改数据 (调用了mutations)
 */


export const state = () => ({
  history: []
})

export const mutations = {
  push(state, data) {
    console.log(data);
    // {departCity: "广州", departCode: "CAN", destCity: "北京", destCode: "BJS", departDate: "2020-08-31"}
    // {departCity: "广州", departCode: "CAN", destCity: "北京", destCode: "BJS", departDate: "2020-08-31"}

    // 只要 出发城市 和 到达 城市 和 出发时间 都一样 重复的数据
    // unshift
    const index = state.history.findIndex(v => {
      if (
        v.departCity === data.departCity
        && v.destCity === data.destCity
        && v.departDate === data.departDate) {
        // 重复了 
        return true
      } else {
        return false;
      }
    });

    if (index !== -1) {
      // 找到了 先删除
      state.history.splice(index, 1);
    }

  
    state.history.unshift(data);

  }
}